//
//  NetWork.m
//  PCS-IPHONE
//
//  Created by zhouChaoLin on 17/3/11.
//  Copyright © 2017年 Henry. All rights reserved.
//

#import "NetWork.h"
#import "Invertory.h"
#import "Stock.h"

@implementation NetWork

+(void)sumShoppingCart:(NSArray  *)arrProduct
              callBack:(CMIOTRequestSucess)callBack{
    
    NSDictionary  *dicJson=[CMCCIOTIGUnity  getRequestJson:[self   submitShoppingCarParama:arrProduct]];
    [[NetworkFactory  alloc]  startPostRequestWithURL:InsertShoppingCart andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
        
        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
            
            [CMCCIOTTipsCenter showSuccessWithStatus:responseJson[@"message"]];
            callBack(responseJson,nil);
            
        } else{
            
            callBack(nil,[self getError:responseJson[@"message"]]);

        }
        
    }
    andFailBlock:^(NSError *error) {
                                             
        callBack(nil,error);
                                        
    } ];
    
}



+(void)getShoppingCart:(NSString  *)shopCartID
              callBack:(CMIOTRequestSucess)callBack{
    
    NSDictionary  *dicJson=[CMCCIOTIGUnity  getRequestJson:@{@"ShoppingCartID":shopCartID}];
    
    [[NetworkFactory  alloc]  startPostRequestWithURL:GetShoppingCart andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
        
        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
            
            NSArray  *arrData=responseJson[@"Data"];
            
            NSMutableArray  *arrXianXiao=[NSMutableArray  new];
            NSMutableArray  *arrZiTi=[NSMutableArray  new];
            NSMutableArray  *arrYouGou=[NSMutableArray  new];
            
            for (int i=0; i<arrData.count; i++) {
                
                Product  *product=[[Product  alloc]  init];
                [product  setModleDic:arrData[i]];
                if ([product.FSaleType   isEqualToString:@"1"] ) {
                        [arrXianXiao  addObject:product];
                }
                else if([product.FSaleType   isEqualToString:@"2"]){
                    
                    [arrZiTi  addObject:product];
                }
                else{
                                        
                    [arrYouGou  addObject:product];
                }
                
                ;
            }
            
            NSMutableDictionary  *dicData=[[NSMutableDictionary  alloc]  initWithObjectsAndKeys:arrXianXiao,@"xianXiao",arrZiTi,@"ziTi",arrYouGou,@"youGou", nil];
            callBack(dicData,nil);
            
        
        } else{
        
              callBack(nil,[self  getError:responseJson[@"message"]]);

        }
        
    }
    andFailBlock:^(NSError *error) {
                                         
              callBack(nil,error);
                                         
                                         
    } ];

}


+(void)delProduc:(NSInteger )FID
        callBack:(CMIOTRequestSucess)callBack{

    NSNumber  *fID=[NSNumber numberWithInteger:FID];
    NSDictionary  *dicJson=[CMCCIOTIGUnity getRequestJson:@{@"FEntryID":fID}];
    
    [[NetworkFactory  alloc]  startPostRequestWithURL:DelShopping andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
        
        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
            
             [CMCCIOTTipsCenter showSuccessWithStatus:responseJson[@"message"]];
             callBack(responseJson,nil);
            
        } else{
            
           callBack(nil,[self  getError:responseJson[@"message"]]);

        }
        
    }
     
    andFailBlock:^(NSError *error) {
                                             
                 callBack(nil,error);
                                             
                                             
                                         } ];

    
    



}

+(void)modProduc:(NSInteger )FID
            FQty:(NSInteger )FQty
        callBack:(CMIOTRequestSucess)callBack{
    
    NSNumber  *fID=[NSNumber numberWithInteger:FID];
    NSNumber  *fQty=[NSNumber numberWithInteger:FQty];
    NSDictionary  *dicJson=[CMCCIOTIGUnity getRequestJson:@{@"FEntryID":fID,@"FQty":fQty}];
    
    [[NetworkFactory  alloc]  startPostRequestWithURL:modifyShopping andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
        
        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
            
            
            callBack(responseJson,nil);
            
        } else{
            
            callBack(nil,[self  getError:responseJson[@"message"]]);
            
        }
        
    }
     
                                         andFailBlock:^(NSError *error) {
                                             
                                             callBack(nil,error);
                                             
                                             
                                         } ];
    
    
    
    
    
    
}


+(void)SubmitOrder:(NSDictionary  *)dicData
          callBack:(CMIOTRequestSucess)callBack{

    NSArray *arrXianXiao=dicData[@"xianXiao"];
    NSArray *arrZiTi=dicData[@"ziTi"];
    NSArray *arrYouGou=dicData[@"youGou"];
    if (arrXianXiao.count==0&&arrZiTi.count==0&&arrYouGou.count==0) {
        [CMCCIOTTipsCenter    showErrorWithStatus:@"请添加商品！"];
        return;
        ;
    }
    NSMutableArray  *arrGouWu=[NSMutableArray new];
    if (arrXianXiao.count) {
        NSDictionary  *dicXianXiao=[self  getOrderDic:arrXianXiao];
        [arrGouWu addObject:dicXianXiao];
    }
    
    if (arrZiTi.count) {
        NSDictionary  *dicZiTi=[self  getOrderDic:arrZiTi];
        [arrGouWu addObject:dicZiTi];    }
    
    
    
    for (int i=0; i<arrYouGou.count; i++) {
        NSDictionary  *dicYouGou=[self  getOrderDic:[[NSArray  alloc]  initWithObjects:arrYouGou[i], nil]];
        [arrGouWu addObject:dicYouGou];
    }
    
    NSDictionary  *dicJson=[CMCCIOTIGUnity getRequestJson:@{@"Orders":arrGouWu}];
    
    [[NetworkFactory  alloc]  startPostRequestWithURL:INSERTSALORDER andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {
        
        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
            
            callBack(responseJson,nil);
            
            
        }
        else{
            
            callBack(responseJson,[self  getError:responseJson[@"message"]]);

        }
        
    }
     
    andFailBlock:^(NSError *error) {
                                             
        callBack(nil,error);
    
                                             
    } ];


}

+(void)searchInventoryLaterCallBack:(CMIOTRequestSucess)callBack{

    
    HangBanInfo  *hangBanInfo=[HangBanInfo  currentHangBan];
    NSString  *FFLIGHTNUMBER=hangBanInfo.FFLIGHTNUMBER;
    NSString  *FFLIGHTDATE=hangBanInfo.FFLIGHTDATE;
    
    NSDictionary  *dicParama=@{@"FNUMBER":FFLIGHTNUMBER,@"FFLIGHTDATE":FFLIGHTDATE};
    NSDictionary  *dicJson= [CMCCIOTIGUnity   getRequestJson:dicParama];
    
    [[NetworkFactory  alloc]  startPostRequestWithURL:GETINVENTORY_BACK  andPostValues:dicJson andSuccessBlock:^(NSDictionary *responseJson) {

        if ([responseJson[@"IsSuccess"]  integerValue]==1) {
            
            NSDictionary  *dicData=[self  getStock:responseJson];
            callBack(dicData,nil);
            
        }
        else{
          
            callBack(nil,[self getError:responseJson[@"message"]]);
        }
        
    } andFailBlock:^(NSError *error) {
      
            callBack(nil,[self getError:nil]);
    } ];

}

#pragma mark 参数整理

+(NSDictionary  *)submitShoppingCarParama:(NSArray  *)arrProduc{

    
    HangBanInfo  *hangBan=[HangBanInfo  currentHangBan];
    
    NSMutableDictionary  *dicFProductList=[NSMutableDictionary  new];
    
   
    
    Product  *product=arrProduc[0];
    [dicFProductList setObject:product.remark.FCustomerAddress?:@""  forKey:@"FCustomerAddress"];
    [dicFProductList setObject:product.remark.FCustomerName?:@"" forKey:@"FCustomerName"];
    [dicFProductList setObject:product.remark.FCustomerTel?:@"" forKey:@"FCustomerTel"];
    [dicFProductList setObject:product.remark.FCustomerSeatNum?:@"" forKey:@"FCustomerSeatNum"];
    
     [dicFProductList  setObject:[HangBanInfo  currentHangBan].shoppingCartID?[HangBanInfo  currentHangBan].shoppingCartID:@"0" forKey:@"ShoppingCartID"];
    [dicFProductList setObject:product.FSaleType forKey:@"FSaleType"];
    [dicFProductList setObject:hangBan.FFLIGHTNUMBER forKey:@"FFLIGHTNUMBER"];
    [dicFProductList setObject:hangBan.FFLIGHTDATE forKey:@"FFLIGHTDATE"];
    
    NSMutableArray  *arrProductList=[NSMutableArray new];
    for (int i=0; i<arrProduc.count; i++) {
        
        Product  *product=arrProduc[i];
        NSDictionary  *dicProduct=[product  getSumitShopingRequestDic];
        [arrProductList addObject:dicProduct];
    }
    [dicFProductList setObject:arrProductList forKey:@"FProductList"];

    return dicFProductList;
}

+(NSDictionary  *)getOrderDic:(NSArray  *)arrModle{
    

        NSMutableDictionary  *dicFProductList=[[NSMutableDictionary  alloc]  init];
        [dicFProductList setObject:@"eacb50844fc84a10b03d7b841f3a6278" forKey:@"FBillTypeID"];
    
        [dicFProductList setObject:@"NORMAL" forKey:@"FBusinessType"];
    
    
        [dicFProductList setObject:@"101032" forKey:@"FCustId"];
        
        
        Product  *product=arrModle[0];
        [dicFProductList setObject:[product.remark.FCustomerAddress  nullClass]  forKey:@"FCustomerAddress"];
        [dicFProductList setObject:[product.remark.FCustomerName  nullClass] forKey:@"FCustomerName"];
        [dicFProductList setObject:[product.remark.FCustomerSeatNum  nullClass] forKey:@"FCustomerSeatNum"];
        [dicFProductList setObject:[product.remark.FCustomerTel  nullClass] forKey:@"FCustomerTel"];
    
    
        
        [dicFProductList setObject:[CMCCIOTIGUnity getDateOfyyyyMMdd:[NSDate  date]] forKey:@"FDate"];
        [dicFProductList setObject:@"A" forKey:@"FDocumentStatus"];
        
        HangBanInfo  *hangBan=[HangBanInfo currentHangBan];
        [dicFProductList setObject:hangBan.FFLIGHTDATE forKey:@"FFLIGHTDATE"];
        [dicFProductList setObject:hangBan.FFLIGHTNUMBER forKey:@"FNumber"];
        [dicFProductList setObject:@"XS001" forKey:@"FPriceListId"];
        
        NSMutableArray  *arrProduct=[NSMutableArray new];
        for (int i=0; i<arrModle.count; i++) {
            
            Product  *product=arrModle[i];
            NSDictionary  *dicProduct=[product  getOrderDic];
            [arrProduct addObject:dicProduct];
        }
        [dicFProductList setObject:arrProduct forKey:@"FProductList"];

    
            [dicFProductList setObject:@"1" forKey:@"FSaleOrgId"];
            [dicFProductList setObject: product.FSaleType forKey:@"FSaleType"];
            [dicFProductList  setObject:@"102872" forKey:@"FSalerId"];
            [dicFProductList setObject:@"PRE001" forKey:@"FSettleCurrId"];
            MemberList  *current=[MemberList  currentUsinfo];
            NSString  *userID=[NSString stringWithFormat:@"%ld",(long)current.userID];
            [dicFProductList setObject:userID forKey:@"FUserID"];
            [dicFProductList setObject:current.userName forKey:@"FUserName"];
            [dicFProductList setObject:[NSNumber numberWithInteger:hangBan.FID] forKey:@"FlightID"];
    
        return   dicFProductList;

   
}

+(NSDictionary  *)getStock:(NSDictionary *)responseJson{

        
        NSMutableArray  *arrMainData=[NSMutableArray new];
        
        NSArray  *arrDataStock;
        NSArray  *arrVender;
        if ([responseJson[@"stockList"]  isKindOfClass:[NSArray class]]) {
            arrDataStock=[responseJson[@"stockList"]  mutableCopy] ;
        }
        else{
            
            arrDataStock=@[];
        }
        
        if ([responseJson[@"data"]  isKindOfClass:[NSArray class]]) {
            arrVender=[responseJson[@"data"]  mutableCopy] ;
        }
        else{
            
            arrVender=@[];
        }
        
        [arrDataStock  enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSDictionary  *dicDataParama=obj;
            Stock  *stock=[[Stock  alloc]  init];
            stock.StockName=STRING_FORMAT(dicDataParama[@"StockName"]);
            stock.StockID=[dicDataParama[@"StockID"]  integerValue];
            [arrMainData  addObject:stock];

        }];
        
        NSMutableArray  *arrMuVender=[NSMutableArray new];
        [arrVender  enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSDictionary  *dicDataParama=obj;
            Invertory  *invertory=[[Invertory  alloc]  init];
            invertory.FDispatchQty=[STRING_FORMAT(dicDataParama[@"FInventoryQty"])  integerValue ];
            invertory.FInventoryQty=[STRING_FORMAT(dicDataParama[@"FInventoryQty"])  integerValue ];
            
            invertory.FName=STRING_FORMAT(dicDataParama[@"FName"]);
            invertory.FNumber=STRING_FORMAT(dicDataParama[@"FNumber"]) ;
            invertory.ID=[STRING_FORMAT(dicDataParama[@"ID"])  integerValue];
            invertory.InvoiceGoodNo=STRING_FORMAT(dicDataParama[@"InvoiceGoodNo"]);
            invertory.FRecycleStockID=[dicDataParama[@"FRecycleStockID"]  integerValue];
            [arrMuVender addObject:invertory  ];
            
        }];

        
        NSDictionary  *dic=[[NSDictionary alloc]  initWithObjectsAndKeys:arrMainData,@"arrMain",arrMuVender,@"verder", nil];
        return  dic;
       
        
    }



+(NSError  *)getError:(NSString  *)str{

    NSError  *error=[[NSError  alloc]  initWithDomain:str.length?str:@"获取数据失败" code:-1 userInfo:nil];
    return error;

}

@end
